﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms ;
using Person.Model ;
using IBatisNet.DataMapper;

namespace Person.Service
{
    class PersonService
    {
        ISqlMapper sqlMapper = Service.Mapper.Instance(); 
        //get all
        public IList<Model.Person> RefreshGrid()
        {
            try
            {
                IList<Model.Person> ps = sqlMapper.QueryForList<Model.Person>("GetAllPerson", null);
                return ps;
            }
            catch (Exception exc)
            {
                MessageBox.Show("Exception: " + exc.Message);
                return null;
            }
        }
        //get 1 by id
        public Model.Person GetPerson_ID( int id)
        {
            try
            {
                Model.Person ps = (Model.Person) sqlMapper.QueryForObject("GetSimplePerson_ID", id);
                return ps;
            }
            catch (Exception exc)
            {
                MessageBox.Show("Exception: " + exc.Message);
                return null;
            }
        }
        //insert
        public bool InsertPerson(Model.Person person)
        {
            try
            {
                int count = 0;
                if ((count = (int)sqlMapper.QueryForObject("Count_Name", person)) > 0)
                {
                    MessageBox.Show("Name is exist.");
                    return false;
                }
                else
                {
                    sqlMapper.Insert("InsertPerson", person);
                    return true;
                }
            }
            catch (Exception exc)
            {
                MessageBox.Show("Exception: " + exc.Message);
                return false;
            }
        }
        //update
        public bool UpdatePerson(Model.Name name, Model.Person person)
        {
            try
            {
                if ((name.FirstName != person.FirstName || name.LastName != person.LastName)
                    && ((int)sqlMapper.QueryForObject("Count_Name", person)) > 0)
                {
                    MessageBox.Show("Name is exist.");
                    return false;
                }
                else
                {
                    sqlMapper.Update("UpdatePerson", person);
                    return true;
                }
            }
            catch (Exception exc)
            {
                MessageBox.Show("Exception: " + exc.Message);
                return false;
            }
        }
        //delete
        public bool DeletePerson(int id)
        {
            try
            {
                sqlMapper.Delete("DeletePerson", id);
                return true;
            }
            catch (Exception exc)
            {
                MessageBox.Show("Exception: " + exc.Message);
                return false;
            }
        }
    }
}
